package pers.whj.util.list;

import java.util.Random;

public interface ListOp<T> {
    boolean merge(List<T> list);

    List<T> subList(int offset, int length);

    /**
     * slice operation
     *
     * @param start index (inclusive)
     * @param end   int (inclusive)
     * @param step  the gap
     * @return the sub list
     */
    List<T> subList(int start, int end, int step);

    /**
     * reverse all the elements in list
     */
    void reverse();

    void reverse(int start, int end);

    /**
     * move each element k positions to the right
     *
     * @param k positions to move
     */
    void rotate(int k);

    void rotate(int start, int end, int k);

    void sort();

    void sort(int start, int end);

    /**
     * random disruption
     */
    void shuffle();

    /**
     * random disruption in specified range
     *
     * @param start begin index
     * @param end   end index
     */
    void shuffle(int start, int end);

    /**
     * random disruption in specified range
     *
     * @param arr   specified array
     * @param start index
     * @param end   index
     */
    static void shuffle(Object[] arr, int start, int end) {
        if (end > start) {
            Random random = new Random();
            for (int i = 1; i <= end - start; i++) {
                int tmp = random.nextInt(i + 1);
                if (tmp != i) {
                    Object x = arr[start + i];
                    arr[start + i] = arr[start + tmp];
                    arr[start + tmp] = x;
                }
            }
        }
    }
}